home *** CD-ROM | disk | FTP | other *** search
/ Almathera Ten Pack 3: CDPD 3 / Almathera Ten on Ten - Disc 3: CDPD3.iso / fish / 001-100 / 001-025 / 009 / proff / look.c < prev    next >
C/C++ Source or Header  |  1995-03-17  |  3KB  |  164 lines

  1.  
  2.  
  3.  
  4.  
  5. /*
  6.  * from K&R "The C Programming language"
  7.  * Table lookup routines
  8.  *
  9.  */
  10. #include <stdio.h>
  11. #include "lookup.h"
  12. /*
  13.  * hash - for a hash value for string s
  14.  *
  15.  */
  16. hash(s)
  17. char *s;
  18. {
  19.     int    hashval;
  20.  
  21.     for (hashval = 0; *s != '\0';)
  22.         hashval += *s++;
  23.     return (hashval % HASHMAX);
  24. }
  25.  
  26. /*
  27.  * lookup - lookup for a string s in the hash table
  28.  *
  29.  */
  30. struct hashlist
  31. *lookup(s, hashtab)
  32. char *s;
  33. struct hashlist *hashtab[];
  34. {
  35.     struct hashlist *np;
  36.  
  37.     for (np = hashtab[hash(s)]; np != NULL; np = np->next)
  38.         if (strcmp(s, np->name) == 0)
  39.             return(np);    /* found     */
  40.     return(NULL);        /* not found */
  41. }
  42.  
  43. /*
  44.  * install - install a string name in hashtable and its value def
  45.  * at a given hashtable.
  46.  */
  47. struct hashlist
  48. *install(name,def,hashtab)
  49. char *name;
  50. char *def;
  51. struct hashlist *hashtab[];
  52. {
  53.     int hashval;
  54.     struct hashlist *np, *lookup();
  55.     char *strsave(), *malloc();
  56.  
  57.     if ((np = lookup(name, hashtab)) == NULL) {    /* not found.. */
  58.         np = (struct hashlist *) malloc(sizeof(*np));
  59.                 if (np == NULL)
  60.             return(NULL);
  61.         if ((np->name = strsave(name)) == NULL)
  62.             return(NULL);
  63.         hashval = hash(np->name);
  64.         np->next = hashtab[hashval];
  65.         hashtab[hashval] = np;
  66.     } else                    /* found..     */
  67.         free(np->def);            /* free prev.  */
  68.     if ((np->def = strsave(def)) == NULL)
  69.         return(NULL);
  70.     return(np);
  71. }
  72.  
  73. /*
  74.  * strsave - save string s somewhere
  75.  *
  76.  */
  77. char
  78. *strsave(s)
  79. char *s;
  80. {
  81.     char *p, *malloc();
  82.     register int n;
  83.  
  84.     n = strlen(s) + 1;
  85.     if ((p = malloc(n)) != NULL)
  86.             strcpy(p, s);
  87.     return(p);
  88. }
  89.  
  90. /*
  91.  * lexinstal - instal a string name in hashtable and its value
  92.  *           used by lexical analyser to quickly match a token
  93.  *           and return its lexical value.
  94.  *
  95.  */
  96. struct lexlist
  97. *lexinstal(name,val,flag,lextable)
  98. char *name;
  99. int val;
  100. int flag;
  101. struct lexlist *lextable[];
  102. {
  103.     int hashval;
  104.     struct lexlist *np,*lexlook();
  105.     char *strsave(), *malloc();
  106.  
  107.     if ((np = lexlook(name,lextable)) == NULL) {    /* not found.. */
  108.         np = (struct lexlist *) malloc(sizeof(*np));
  109.         if (np == NULL)
  110.             return(NULL);
  111.         if ((np->name = strsave(name)) == NULL)
  112.             return(NULL);
  113.         hashval = hash(np->name);
  114.         np->link = lextable[hashval];
  115.         lextable[hashval] = np;
  116.     }
  117.     np->val = val;                /* replace prev */
  118.     np->flag = flag;
  119.     return(np);
  120. }
  121.  
  122. /*
  123.  * lexlook - lookup for a string s in the hash table
  124.  *         used by lexinstal only.
  125.  *
  126.  */
  127. struct lexlist
  128. *lexlook(s,table)
  129. char *s;
  130. struct lexlist *table[];
  131. {
  132.     struct lexlist *np;
  133.  
  134.     for (np = table[hash(s)]; np != NULL; np = np->link)
  135.         if (strcmp(s, np->name) == 0)
  136.             return(np);    /* found     */
  137.     return(NULL);        /* not found */
  138. }
  139.  
  140. /*
  141.  * remove an item from the hash table forever
  142.  *
  143.  */
  144. struct lexlist
  145. *remove(s, table)
  146. char *s;
  147. struct lexlist *table[];
  148. {
  149.     struct lexlist *np, *xp;
  150.  
  151.     np = table[hash(s)];
  152.     xp = np; 
  153.     while (np != NULL) {
  154.         if (strcmp(s, np->name) == 0) {
  155.             xp->link = np->link;    /* remove the link */
  156.             return(np);        /* return the lost */
  157.         }
  158.         xp = np; 
  159.         np = np->link;
  160.     }
  161.     return(NULL);
  162. }
  163.  
  164.